<?php

namespace App\Http\Resources\Home\CartResource;

use App\Services\OrderService;
use App\Traits\HelperTrait;
use Illuminate\Http\Resources\Json\ResourceCollection;

class CartCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    use HelperTrait;

    public $sum = [];
    public $is_checked;

    public function toArray($request)
    {
        $this->is_checked = [];
        $this->collection->map(function ($item) {
            if (!empty($item)) {
                $item->carts->map(function ($cartItem) {
                    if (!empty($cartItem)) {
                        if ($cartItem->checked == 1) {
                            $this->sum['order'][] = ['goods_id' => $cartItem->goods_id, 'sku_id' => $cartItem->sku_id, 'buy_num' => $cartItem->buy_num];
                        }
                        $this->is_checked[$cartItem->store_id]['is_checked'] = 1;
                        if ($cartItem->checked == 0) {
                            $this->is_checked[$cartItem->store_id]['is_checked'] = 0;
                        }
                    }
                });
            }
        });
        $total_price = 0;
        $coupon_money = 0;
        $goods_market_price = 0;

        if (!empty($this->sum)) {
            $orderService = new OrderService();
            $price = $orderService->CalculatePrice($this->sum);
            extract($price['data'], EXTR_OVERWRITE);
        }

        return [
            'data' => $this->collection->map(function ($item) {
                return [
                    'store_id' => $item->store->id,
                    'user_id' => $item->user_id,
                    'store_name' => $item->store->store_name,
                    'store_logo' => $item->store->store_logo,
                    'checked' => $this->is_checked[$item->store->id]['is_checked'],
                    'css' => false,
                    'cart_list' => $item->carts->map(function ($cartItem) {

                        // 是否存在sku
                        $goods_image = $this->thumb($cartItem->goods->goods_master_image, 150);
                        $goods_price = $cartItem->goods->goods_price;
                        $sku_name = '-';
                        if (!empty($cartItem->goods_sku)) {
                            $goods_price = $cartItem->goods_sku->goods_price;
                            $sku_name = $cartItem->goods_sku->sku_name;
                        }
                        return [
                            'cart_id' => $cartItem->id,
                            'goods_id' => $cartItem->goods_id,
                            'sku_id' => $cartItem->sku_id,
                            'goods_name' => $cartItem->goods->goods_name,
                            'buy_num' => $cartItem->buy_num,
                            'goods_image' => $goods_image,
                            'goods_price' => $goods_price,
                            'sku_name' => $sku_name,
                            'checked' => $cartItem->checked,
                        ];
                    }),
                ];
            }),

            'total_price' => $total_price,
            'coupon_money' => $coupon_money,
            'goods_market_price' => $goods_market_price
            // 'data'=>$this->collection,
//            'total' => $this->total(), // 数据总数
//            'per_page' => $this->perPage(), // 每页数量
//            'current_page' => $this->currentPage(), // 当前页码
        ];

    }
}
